/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.nct.config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 *
 * @author annn
 */
public class Config {

    public String propertiesFile;
    public static Config _Instance;
    InputStream inputStream = null;

    public Config() {
        this.propertiesFile = "config.properties";
    }

    public Config(String propertiesFile) {
        this.propertiesFile = propertiesFile;
    }

    public static Config getInstance() {
        if (_Instance == null) {
            return _Instance = new Config();
        }
        return _Instance;
    }

    public static Config getInstance(String propertiesFile) {
        if (_Instance == null) {
            return _Instance = new Config(propertiesFile);
        }
        return _Instance;
    }

    private Properties Init() throws FileNotFoundException, IOException {
        Properties prop = new Properties();
//      String propFileName = "config.properties";
        String propFileName = propertiesFile;

        inputStream = new FileInputStream(propFileName);
        prop.load(inputStream);
        if (inputStream == null) {
            throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
        }
        return prop;
    }

    /*
     Get Value
     */
    public String getPropValues(String Key) throws IOException {
        Properties prop = Init();
        String value = prop.getProperty(Key);
        inputStream.close();
        return value;
    }

    /*
     Set value 
     */
    public Config setPropValues(String Key, String Value) throws IOException {
        Properties prop = Init();
        prop.setProperty(Key, Value);
        inputStream.close();
        return this;
    }
}
